package cn.scw.project.facility.controller;

import cn.scw.common.ValidateUtil;
import cn.scw.common.constant.Constants;
import cn.scw.common.constant.SysConsts;
import cn.scw.common.exception.CustomException;
import cn.scw.common.utils.pay.MD5Util;
import cn.scw.framework.aspectj.lang.annotation.Log;
import cn.scw.framework.aspectj.lang.enums.BusinessType;
import cn.scw.framework.config.ServerConfig;
import cn.scw.framework.redis.RedisCache;
import cn.scw.framework.web.controller.BaseController;
import cn.scw.framework.web.domain.AjaxResult;
import cn.scw.framework.web.page.TableDataInfo;
import cn.scw.project.facility.domain.netework.bean.FacPrintOrderAddReq;
import cn.scw.project.facility.domain.netework.bean.FacPrintOrderListReq;
import cn.scw.project.facility.domain.vo.FacPrintOrderVo;
import cn.scw.project.facility.service.IFacPrintOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.jdom.JDOMException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 打印订单Controller
 *
 * @author api
 * @date 2020-05-20
 */
@Api(tags = "打印订单")
@RestController
@RequestMapping(SysConsts.CONTEXT_PATH + "/facility/printOrder")
public class FacPrintOrderController extends BaseController {

    @Autowired
    private IFacPrintOrderService facPrintOrderService;

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private RedisCache redisCache;

    @ApiOperation("生成word文档")
    @GetMapping("/word")
    public AjaxResult word(@RequestParam String content, @RequestParam int size) {
        String fileUrl = facPrintOrderService.word(content, size);
        return AjaxResult.success(serverConfig.getUrl() + Constants.RESOURCE_PREFIX + fileUrl);
    }

    @ApiOperation("打印端查询打印列表")
    @GetMapping("/print/list/{code}")
    public TableDataInfo printList(@PathVariable @ApiParam("设备编码") String code) {
        List<FacPrintOrderVo> list = facPrintOrderService.selectFacPrintOrderPrintList(code);
        return getDataTable(list);
    }

    @ApiOperation("打印端更新打印状态")
    @PutMapping("/print/{id}")
    public AjaxResult print(@PathVariable @ApiParam("订单id") Long id) {
        return toAjax(facPrintOrderService.print(id));
    }

    @ApiOperation("查询打印订单列表")
    //@PreAuthorize("@ss.hasPermi('facility:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(FacPrintOrderListReq req) {
//        startPage();
        if (req != null) {
            logger.info("TableDataInfo facilityCode=" + req.getFacilityCode());
        }
        List<FacPrintOrderVo> list = facPrintOrderService.selectFacPrintOrderList(req);
        return getDataTable(list);
    }

    @ApiOperation("查询个人用户打印记录")
    //@PreAuthorize("@ss.hasPermi('facility:order:list')")
    @GetMapping("/wx/list")
    public TableDataInfo list() {
        startPage();
        List<FacPrintOrderVo> list = facPrintOrderService.selectFacPrintOrderListByUser();
        return getDataTable(list);
    }

    @ApiOperation("获取打印订单详细信息")
    //@PreAuthorize("@ss.hasPermi('facility:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(facPrintOrderService.selectFacPrintOrderVoById(id));
    }

    @ApiOperation("新增打印订单")
    //@PreAuthorize("@ss.hasPermi('facility:order:add')")
    @Log(title = "打印订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(FacPrintOrderAddReq req) throws JDOMException, IOException {
        if (req != null) {
            String strType = req.getType();
            int nType = 0;
            if (strType != null && !strType.equalsIgnoreCase("")) nType = Integer.parseInt(strType);

            String redisKey = MD5Util.MD5Encode(req.getFacilityCode() + req.getContent(), "utf-8");
            Integer printPageNum = redisCache.getCacheObject(redisKey);
            if (printPageNum == null) {
                printPageNum = ValidateUtil.wordPage(req.getContent(), nType);
                if (printPageNum < 1) throw new CustomException("打印页数不能少于1页");
            }
            req.setEndPage(printPageNum);
            logger.info("add startpage=" + req.getStartPage() + ", endpage=" + req.getEndPage());
            if (req.getFacilityCode() != null) {
                System.out.println("FacPrintOrderAddReq fac=" + req.getFacilityCode());
            } else {
                System.out.println("FacPrintOrderAddReq fac is null");
            }

        } else {
            System.out.println("FacPrintOrderAddReq req is null");
        }
        return facPrintOrderService.insertFacPrintOrder(req);
    }

    @ApiOperation("余额支付")
    @PostMapping("/pay")
    public AjaxResult pay(@RequestParam @ApiParam("订单id") Long id, @RequestParam @ApiParam("密码") String payPassword) {
        return toAjax(facPrintOrderService.pay(id, payPassword));
    }

    @ApiOperation("更换底色")
    @GetMapping("/imgSet")
    public AjaxResult imgSet(@RequestParam @ApiParam("订单id") String path,
                             @RequestParam @ApiParam("颜色r") int r,
                             @RequestParam @ApiParam("颜色r") int g,
                             @RequestParam @ApiParam("颜色r") int b) {
        return AjaxResult.success(facPrintOrderService.imgSet(path, r, g, b));
    }

//    /**
//     * 修改打印订单
//     */
//    //@PreAuthorize("@ss.hasPermi('facility:order:edit')")
//    @Log(title = "打印订单", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody FacPrintOrder facPrintOrder) {
//        return toAjax(facPrintOrderService.updateFacPrintOrder(facPrintOrder));
//    }

//    /**
//     * 删除打印订单
//     */
//    //@PreAuthorize("@ss.hasPermi('facility:order:remove')")
//    @Log(title = "打印订单", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids) {
//        return toAjax(facPrintOrderService.deleteFacPrintOrderByIds(ids));
//    }
}
